<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="监测类型" prop="monitoringType">
        <el-select v-model="queryParams.monitoringType" placeholder="请选择监测类型" clearable style="width: 200px">
          <el-option
            v-for="dict in monitor_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="边坡编号" prop="slopeCode">
        <el-select v-model="queryParams.slopeCode" placeholder="请选择边坡编号" clearable style="width: 200px">
          <el-option
              v-for="dict in slopeCodeList"
              :key="dict.value"
              :label="dict.value"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['slope:monitorpoints:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['slope:monitorpoints:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['slope:monitorpoints:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['slope:monitorpoints:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table scrollbar-always-on v-loading="loading" :data="monitorpointsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监测点ID" fixed="left" align="center" prop="monitorId" width="280px"/>
      <el-table-column label="边坡编号" fixed="left" align="center" prop="slopeCode" width="280px">
        <template #default="scope">
          <dict-tag :options="slopeCodeList" :value="scope.row.slopeCode"/>
        </template>
      </el-table-column>
      <el-table-column label="测点编号" align="center" prop="pointCode" width="280px"/>
      <el-table-column label="测点名称" align="center" prop="pointName" width="220px"/>
      <el-table-column label="设备ID" align="center" prop="deviceId" width="280px"/>
      <el-table-column label="监测类型" align="center" prop="monitoringType" width="180px">
        <template #default="scope">
          <dict-tag :options="monitor_type" :value="scope.row.monitoringType"/>
        </template>
      </el-table-column>
      <el-table-column label="监测内容" align="center" prop="monitoringContent" />
      <el-table-column label="采集器编码" align="center" prop="collectorCode" width="100px"/>
      <el-table-column label="通道" align="center" prop="channel" />
      <el-table-column label="位置" align="center" prop="location" width="180px">
        <template #default="scope">
          <dict-tag :options="monitor_area" :value="scope.row.location"/>
        </template>
      </el-table-column>
      <el-table-column label="数据单位" align="center" prop="dataUnit" width="80px">
        <template #default="scope">
          <dict-tag :options="data_unit" :value="scope.row.dataUnit"/>
        </template>
      </el-table-column>
      <el-table-column label="数据精度" align="center" prop="dataPrecision" width="80px">
        <template #default="scope">
          <dict-tag :options="data_precision" :value="scope.row.dataPrecision"/>
        </template>
      </el-table-column>
      <el-table-column label="管养单位" align="center" prop="deptId" width="180">
        <template #default="scope">
          <dict-tag :options="sys_dept_list" :value="scope.row.deptId"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['slope:monitorpoints:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['slope:monitorpoints:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监测点信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="monitorpointsRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="监测点ID" prop="monitorId">
              <el-input v-model="form.monitorId" placeholder="请输入监测点ID" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="边坡编号" prop="slopeCode">
              <el-select v-model="form.slopeCode" placeholder="请选择边坡编号">
                <el-option
                    v-for="dict in slopeCodeList"
                    :key="dict.value"
                    :label="dict.value"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="测点编号" prop="pointCode">
              <el-input v-model="form.pointCode" placeholder="请输入测点编号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="测点名称" prop="pointName">
              <el-input v-model="form.pointName" placeholder="请输入测点名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="设备ID" prop="deviceId">
              <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="监测类型" prop="monitoringType">
              <el-select v-model="form.monitoringType" placeholder="请选择监测类型">
                <el-option
                    v-for="dict in monitor_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="监测内容" prop="monitoringContent">
              <el-input v-model="form.monitoringContent" placeholder="请输入监测内容" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="采集器编码" prop="collectorCode">
              <el-input v-model="form.collectorCode" placeholder="请输入采集器编码" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通道" prop="channel">
              <el-input v-model="form.channel" placeholder="请输入通道" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="位置" prop="location">
              <el-select v-model="form.location" placeholder="请选择位置">
                <el-option
                    v-for="dict in monitor_area"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="数据单位" prop="dataUnit">
              <el-select v-model="form.dataUnit" placeholder="请选择数据单位">
                <el-option
                    v-for="dict in data_unit"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="数据精度" prop="dataPrecision">
              <el-select v-model="form.dataPrecision" placeholder="请选择数据精度">
                <el-option
                    v-for="dict in data_precision"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="管养单位" prop="deptId">
              <el-select v-model="form.deptId" placeholder="请选择管养单位">
                <el-option
                    v-for="dict in sys_dept_list"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Monitorpoints">
import { listMonitorpoints, getMonitorpoints, delMonitorpoints, addMonitorpoints, updateMonitorpoints } from "@/api/slope/monitorpoints/monitorpoints.js";
import { listSlopeCode } from "@/api/slope/info/info.js"

const { proxy } = getCurrentInstance();
const { monitor_type,sys_dept_list,monitor_area,data_unit,data_precision } = proxy.useDict('monitor_type','sys_dept_list','monitor_area','data_unit','data_precision');

const slopeCodeList = ref([])
const monitorpointsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    slopeCode: null,
    monitoringType: null,
  },
  rules: {
    monitorId: [
      { required: true, message: "监测点ID不能为空", trigger: "blur" }
    ],
    slopeCode: [
      { required: true, message: "边坡编号不能为空", trigger: "change" }
    ],
    pointCode: [
      { required: true, message: "测点编号不能为空", trigger: "blur" }
    ],
    pointName: [
      { required: true, message: "测点名称不能为空", trigger: "blur" }
    ],
    deviceId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    monitoringType: [
      { required: true, message: "监测类型不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询监测点信息列表 */
function getList() {
  loading.value = true;
  listSlopeCode().then(response => {
    // 假设 response 是一个普通的对象
    slopeCodeList.value = Object.entries(response).map(([_, value]) => ({
      label: value,
      value: value
    }))
  })
  listMonitorpoints(queryParams.value).then(response => {
    monitorpointsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    monitorId: null,
    slopeCode: null,
    pointCode: null,
    pointName: null,
    deviceId: null,
    monitoringType: null,
    monitoringContent: null,
    collectorCode: null,
    channel: null,
    location: null,
    dataUnit: null,
    dataPrecision: null,
    remarks: null,
    deptId: null
  };
  proxy.resetForm("monitorpointsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加监测点信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMonitorpoints(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改监测点信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["monitorpointsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMonitorpoints(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMonitorpoints(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除监测点信息编号为"' + _ids + '"的数据项？').then(function() {
    return delMonitorpoints(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('slope/monitorpoints/export', {
    ...queryParams.value
  }, `monitorpoints_${new Date().getTime()}.xlsx`)
}

getList();
</script>
